//
//  ScoutView.m
//  ScoutsDemo
//
//  Created by tdh1 on 01/09/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ScoutView.h"
#import "UIDataRecorder.h"
#import <AudioToolbox/AudioToolbox.h>

@interface TestCase : NSObject
{
	int conditionCase;
	CGRect washerPos;
	CGRect washerHole;
	CGRect pegPlacement;
}

@property int conditionCase;
@property CGRect washerPos;
@property CGRect washerHole;
@property CGRect pegPlacement;

@end


@implementation TestCase
@synthesize washerPos, washerHole, pegPlacement, conditionCase;

@end

#define washerStartPosTL CGRectMake(0,20, 100, 100);
#define washerStartPosBR CGRectMake(220,380, 100, 100);
#define washerStartPosTR CGRectMake(220,20, 100, 100);
#define washerStartPosBL CGRectMake(0,380, 100, 100);

#define washerHoleLarge CGRectMake(10,10, 80, 80);
#define washerHoleMedLarge CGRectMake(15,15, 70, 70);
#define washerHoleMedium CGRectMake(20,20, 60, 60);
#define washerHoleSmall CGRectMake(25,25, 50, 50);

//WASHER TOP LEFT
//Angular from bot right
#define d_1 CGRectMake(320 - 100, 480 - 100, 40, 40)
#define d_2 CGRectMake(270 - 100, 430 - 100, 40, 40)
#define d_3 CGRectMake(220 - 100, 380 - 100, 40, 40)
#define d_4 CGRectMake(170 - 100, 330 - 100, 40, 40)

//Vert from bot left
#define d_5 CGRectMake(30, 480 - 63, 40, 40)
#define d_6 CGRectMake(30, 480 - (63 * 2), 40, 40)
#define d_7 CGRectMake(30, 480 - (63 * 3), 40, 40)
#define d_8 CGRectMake(30, 480 - (63 * 4), 40, 40)

//Horiz from top left
#define d_9 CGRectMake(320 - 55, 50, 40, 40)
#define d_10 CGRectMake(320 - (2 * 55), 50, 40, 40)
#define d_11 CGRectMake(320 - (3 * 55), 50, 40, 40)
#define d_12 CGRectMake(320 - (4 * 55), 50, 40, 40)


//WASHER BOT RIGHT
//Angular from top left
#define d_13 CGRectMake(100, 100, 40, 40)
#define d_14 CGRectMake(320 - (270 - 100), 480 - (430 - 100), 40, 40)
#define d_15 CGRectMake(320 - (220 - 100), 480 - (380 - 100), 40, 40)
#define d_16 CGRectMake(320 - (170 - 100), 480 - (330 - 100), 40, 40)

//Vert from top right
#define d_17 CGRectMake(260, 480 - (480 - 63), 40, 40)
#define d_18 CGRectMake(260, 480 - (480 - (63 * 2)), 40, 40)
#define d_19 CGRectMake(260, 480 - (480 - (63 * 3)), 40, 40)
#define d_20 CGRectMake(260, 480 - (480 - (63 * 4)), 40, 40)

//Horiz from bot left
#define d_21 CGRectMake(55 - 40, 420, 40, 40)
#define d_22 CGRectMake((2 * 55) - 40, 420, 40, 40)
#define d_23 CGRectMake((3 * 55) - 40, 420, 40, 40)
#define d_24 CGRectMake((4 * 55) - 40, 420, 40, 40)

@implementation ScoutView

@synthesize soundFileURLRef;
@synthesize soundFileObject;

#define tl 0
#define br 1
#define tr 2
#define bl 3

#define horizontal 0
#define vertical 1
#define angular 2


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
		
		testIndex = 0;
		
		pegSize = 40;
		
		washerHoleSize = 50;
		
		tests = [[NSMutableArray alloc] init];
		
		NSMutableArray *temp = [NSMutableArray arrayWithCapacity:16];
		
//		int i = 0;
//		int j = 0;
		
		//NSInteger direction = 0;
		
		NSInteger conditionCounter = 0;
		NSInteger directionGroup = 0;
		
		while (directionGroup < 4){
			
			NSInteger movement = 0;
			
			while (movement < 3){
				
				NSInteger washerSize = 0;
				
				while (washerSize < 4){
					
					NSInteger pegDistance = 1;
					
					while (pegDistance < 5){
						
						TestCase *one = [[TestCase alloc] init];
						
						one.conditionCase = conditionCounter;
						
						switch (directionGroup) {
							case tl:
								one.washerPos = washerStartPosTL;
								break;
							case br:
								one.washerPos = washerStartPosBR;
								break;
							case tr:
								one.washerPos = washerStartPosTR;
								break;
							case bl:
								one.washerPos = washerStartPosBL;
								break;
						}
						
						switch (movement) {
							case horizontal:
								
							//	NSLog(@"H");
							//	one.pegPlacement = CGRectMake(50, 50, 40, 40);
							//	break;
								
								switch (directionGroup) {
									case tl:
										one.pegPlacement = CGRectMake(320 - (55 * pegDistance), 50, 40, 40);
										break;
									case br:
										one.pegPlacement = CGRectMake((55 * pegDistance) - 40, 420, 40, 40);
										break;
									case tr:
										one.pegPlacement = CGRectMake((55 * pegDistance) - 40, 50, 40, 40);
										break;
									case bl:
										one.pegPlacement = CGRectMake(320 - (55 * pegDistance), 420, 40, 40);
										break;
								}
								
								break;
							case vertical:
								
								//NSLog(@"V");
								//one.pegPlacement = CGRectMake(50, 50, 40, 40);
								//break;
								
								switch (directionGroup) {
									case tl:
										one.pegPlacement = CGRectMake(30, 480 - (63 * pegDistance), 40, 40);
										break;
									case br:
										one.pegPlacement = CGRectMake(260, (63 * pegDistance), 40, 40);
										break;
									case tr:
										one.pegPlacement = CGRectMake(260, 480 - (63 * pegDistance), 40, 40);
										break;
									case bl:
										one.pegPlacement = CGRectMake(30, (63 * pegDistance), 40, 40);
										break;
								}
								
								break;
							case angular:
								
								//NSLog(@"A");
								//one.pegPlacement = CGRectMake(50, 50, 40, 40);
								//break;
								
								switch (directionGroup) {
									case tl:
										one.pegPlacement = CGRectMake(320 - (55 * pegDistance), 480 - (63 * pegDistance), 40, 40);
										break;
									case br:
										one.pegPlacement = CGRectMake((55 * pegDistance) - 40, (63 * pegDistance), 40, 40);
										break;
									case tr:
										one.pegPlacement = CGRectMake((55 * pegDistance) - 40, 480 - (63 * pegDistance), 40, 40);
										break;
									case bl:
										one.pegPlacement = CGRectMake(320 - (55 * pegDistance), (63 * pegDistance), 40, 40);
										break;
								}
								
								break;
						}
						
						switch (washerSize) {
							case 0:
								one.washerHole = washerHoleLarge;
								break;
							case 1:
								one.washerHole = washerHoleMedLarge;
								break;
							case 2:
								one.washerHole = washerHoleMedium;
								break;
							case 3:
								one.washerHole = washerHoleSmall;
								break;
						}
						
						[temp addObject:one];
						
						conditionCounter++;
						pegDistance++;
					}
					
					washerSize++;
				}
				
				movement++;
			}
			
			directionGroup++;
			
		}
		
		
		/*		while (direction < 6){
		 
		 int k = 0;
		 
		 while (k < 16){
		 TestCase *one = [[TestCase alloc] init];
		 
		 one.conditionCase = i;
		 
		 if (direction == 0){
		 one.washerPos = washerStartPosTL;
		 if (k < 4) {
		 one.pegPlacement = d_1;
		 } else if (k < 8) {
		 one.pegPlacement = d_2;
		 } else if (k < 12) {
		 one.pegPlacement = d_3;
		 } else {
		 one.pegPlacement = d_4;
		 }
		 
		 } else if (direction == 1){
		 one.washerPos = washerStartPosTL;
		 if (k < 4) {
		 one.pegPlacement = d_5;
		 } else if (k < 8) {
		 one.pegPlacement = d_6;
		 } else if (k < 12) {
		 one.pegPlacement = d_7;
		 } else {
		 one.pegPlacement = d_8;
		 }
		 } else if (direction == 2){
		 one.washerPos = washerStartPosTL;
		 if (k < 4) {
		 one.pegPlacement = d_9;
		 } else if (k < 8) {
		 one.pegPlacement = d_10;
		 } else if (k < 12) {
		 one.pegPlacement = d_11;
		 } else {
		 one.pegPlacement = d_12;
		 }
		 } else if (direction == 3){
		 one.washerPos = washerStartPosBR;
		 if (k < 4) {
		 one.pegPlacement = d_13;
		 } else if (k < 8) {
		 one.pegPlacement = d_14;
		 } else if (k < 12) {
		 one.pegPlacement = d_15;
		 } else {
		 one.pegPlacement = d_16;
		 }
		 
		 } else if (direction == 4){
		 one.washerPos = washerStartPosBR;
		 if (k < 4) {
		 one.pegPlacement = d_17;
		 } else if (k < 8) {
		 one.pegPlacement = d_18;
		 } else if (k < 12) {
		 one.pegPlacement = d_19;
		 } else {
		 one.pegPlacement = d_20;
		 }
		 } else if (direction == 5){
		 one.washerPos = washerStartPosBR;
		 if (k < 4) {
		 one.pegPlacement = d_21;
		 } else if (k < 8) {
		 one.pegPlacement = d_22;
		 } else if (k < 12) {
		 one.pegPlacement = d_23;
		 } else {
		 one.pegPlacement = d_24;
		 }
		 }
		 
		 
		 if (j == 0){
		 one.washerHole = w_1;
		 } else if (j == 1){
		 one.washerHole = w_2;
		 } else if (j == 2){
		 one.washerHole = w_3;
		 } else if (j == 3){
		 one.washerHole = w_4;
		 }
		 
		 [temp addObject:one];
		 
		 j++;
		 if (j > 3) j = 0;
		 i++;
		 k++;
		 }
		 direction++;
		 }
		 */
		srandom([[NSDate date] timeIntervalSince1970]);
		
		// Get the main bundle for the app
		CFBundleRef mainBundle;
		mainBundle = CFBundleGetMainBundle ();
		
		// Get the URL to the sound file to play
		soundFileURLRef  =	CFBundleCopyResourceURL (
													 mainBundle,
													 CFSTR ("tap"),
													 CFSTR ("wav"),
													 NULL
													 );
		
		// Create a system sound object representing the sound file
		AudioServicesCreateSystemSoundID (
										  soundFileURLRef,
										  &soundFileObject
										  );
		
		NSLog(@"There are %d Conditions", conditionCounter);
		
		while ([temp count]){
			int index = random()%[temp count]; 
			[tests addObject: [temp objectAtIndex:index]];
			[temp removeObjectAtIndex:index];
		}

/*		while ([temp count]){
			[tests addObject: [temp lastObject]];
			[temp removeLastObject];
		}
*/
		
		
/*		while ([temp count]){
			[tests addObject: [temp objectAtIndex:0]];
			[temp removeObjectAtIndex:0];
		}
*/		
		
		//CGRect ourWasher = CGRectMake(/*random()%320, random()%460*/0,20, 100, 100);
		
		washer = CGRectMake([[tests objectAtIndex:testIndex] washerPos].origin.x,[[tests objectAtIndex:testIndex] washerPos].origin.y,[[tests objectAtIndex:testIndex] washerPos].size.width,[[tests objectAtIndex:testIndex] washerPos].size.height);
		
		washerHoleRect = CGRectMake(washer.origin.x + [[tests objectAtIndex:testIndex] washerHole].origin.x, washer.origin.y + [[tests objectAtIndex:testIndex] washerHole].origin.y, [[tests objectAtIndex:testIndex] washerHole].size.width, [[tests objectAtIndex:testIndex] washerHole].size.height);
		
		CGRect ourPeg = [[tests objectAtIndex:testIndex] pegPlacement]; //CGRectMake(random()%320, random()%460, pegSize, pegSize);
		
		peg = ourPeg;
		
		grabOffset = CGPointMake(-99,-99);
		
		[self setMultipleTouchEnabled:NO];
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
    CGContextRef ctxt = UIGraphicsGetCurrentContext();
	
	[[UIColor blackColor] set];
	
	CGContextFillRect(ctxt, self.frame);
	
	[[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.35] set];
	
	[[NSString stringWithFormat: @"%.0f%% Complete", (testIndex/192.0)*100.0] drawAtPoint:CGPointMake(110, 220) forWidth:100 withFont:[UIFont fontWithName:@"Helvetica" size:14] fontSize:14 lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignCenters];
	
	[[UIColor whiteColor] set];
	
	CGContextFillEllipseInRect(ctxt, peg);
	
	[[UIColor whiteColor] set];
	
	CGContextSaveGState(ctxt);
	
	CGMutablePathRef path = CGPathCreateMutable();
	CGPathAddEllipseInRect(path, nil, washer);
	CGPathAddEllipseInRect(path, nil, washerHoleRect);
	CGContextAddPath(ctxt, path);
	
	CGContextEOClip(ctxt);
	
	CGContextFillEllipseInRect(ctxt, washer);
	
	CGContextRestoreGState(ctxt);
	
	CGPathRelease(path);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
	[super touchesBegan:touches withEvent:event];
	
	for (UITouch *t in touches){
		
		CGPoint touchPoint = [t locationInView: self];
		
		if(CGRectContainsPoint(washer/*i.frame*/, touchPoint)) {
			[[UIDataRecorder sharedDataRecorder] addCustomEvenType: @"Grabbed_Washer" withTouches:touches andEvent:event];
			[[UIDataRecorder sharedDataRecorder] beginCondition: [NSString stringWithFormat: @"%d",[[tests objectAtIndex:testIndex] conditionCase]]];
			//			[[UIDataRecorder sharedDataRecorder] captureScreenShot];
			
			grabOffset = CGPointMake(washer.origin.x - touchPoint.x, washer.origin.y - touchPoint.y);
			break;
		}
	}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
	//[[UIDataRecorder sharedDataRecorder] captureScreenShot];
	[super touchesEnded:touches withEvent:event];
	
	if (CGRectContainsRect(washerHoleRect, peg)){
		
		AudioServicesPlayAlertSound (self.soundFileObject);
		
		[[UIDataRecorder sharedDataRecorder] addCustomEvenType: @"Pegged_Washer" withTouches:touches andEvent:event];
		
		//if (testIndex < [tests count] - 1) testIndex++;
		testIndex++;
		
		//		CGRect ourWasher = CGRectMake(0,20, 100, 100);
		
		//		washer = ourWasher;
		
		
		NSLog(@"%d of %d",testIndex, [tests count]);
		
		if (testIndex == [tests count]){
			exit(0);
		}
		
		washer = CGRectMake([[tests objectAtIndex:testIndex] washerPos].origin.x,[[tests objectAtIndex:testIndex] washerPos].origin.y,[[tests objectAtIndex:testIndex] washerPos].size.width,[[tests objectAtIndex:testIndex] washerPos].size.height);
		
		washerHoleRect = CGRectMake(washer.origin.x + [[tests objectAtIndex:testIndex] washerHole].origin.x, washer.origin.y + [[tests objectAtIndex:testIndex] washerHole].origin.y, [[tests objectAtIndex:testIndex] washerHole].size.width, [[tests objectAtIndex:testIndex] washerHole].size.height);
		
		CGRect ourPeg = [[tests objectAtIndex:testIndex] pegPlacement];
		
		peg = ourPeg;
		
		grabOffset = CGPointMake(-99,-99);
		
		[self setNeedsDisplay];
		
		//[self setAlpha:1.0];
		
		[[UIDataRecorder sharedDataRecorder] closeEventEpoch];
				
		//if (testIndex == [tests count]) exit(0);
		
		return;
	}
	
	grabOffset = CGPointMake(-99,-99);
}



- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
	for (UITouch *t in touches){
		
		CGPoint touchPoint = [t locationInView: self];
		
		if(grabOffset.x != -99 && grabOffset.y != -99) {
			washer = CGRectMake(touchPoint.x + grabOffset.x, touchPoint.y + grabOffset.y, 100, 100);
			washerHoleRect = CGRectMake(washer.origin.x + [[tests objectAtIndex:testIndex] washerHole].origin.x, washer.origin.y + [[tests objectAtIndex:testIndex] washerHole].origin.y, [[tests objectAtIndex:testIndex] washerHole].size.width, [[tests objectAtIndex:testIndex] washerHole].size.height);
			[self setNeedsDisplay];
			break;
		}
		
		/*
		 for (UIImageView *i in self.subviews){
		 if(CGRectContainsPoint(i.frame, touchPoint)) {
		 [self bringSubviewToFront: i];
		 [i setFrame:CGRectMake(touchPoint.x - 50, touchPoint.y - 50, 100, 100)];
		 break;
		 }
		 }
		 */
	}
	
	[super touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{
	[super touchesCancelled:touches withEvent:event];
}

- (void)dealloc {
    [super dealloc];
}


@end
